<?php

class indexAction extends FirstendAction
{
    public function _initialize()
    {
        parent::_initialize();
        $reurl = $_SERVER['REQUEST_URI'];
        $reurl = str_replace('item', 'detail', $reurl);
        $reurl = str_replace('cate', 'list/index', $reurl);
        if ($this->isMobile()) {
            redirect(C('yh_headerm_html') . $reurl);
        }
        $this->_mod = D('items');
        $this->_ad = D('ad')->cache(true, 10 * 60);
        C('DATA_CACHE_TIME', C('yh_site_cachetime'));
        $topad = $this->_ad->where('status=2')->find();
        if ($topad) {
            $this->assign('topad', $topad);
        }
    }


    private function _alert_adv()
    {
        if (cookie("alertad") != "yes") {
            cookie("alertad", "yes", 3600 * 12);
            $ad = M('ad')->where('status=3')->find();
            return $ad;
        }

    }

    public function index()
    {
        $this->assign('alertadv', $this->_alert_adv());

        $ad = $this->_ad->where(array(
            'status' => '0'
        ))
            ->order('id desc')
            ->select();
        $this->assign('ad_list', $ad);
        $today_wh['pass'] = '1';
        $today_wh['isshow'] = '1';
        $today_wh['status'] = 'underway';
        $today_str = mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        $tomorr_str = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y"));
        $today_wh['add_time'] = array(
            array(
                'egt',
                $today_str
            ),
            array(
                'elt',
                $tomorr_str
            )
        );
        $today_list = $this->_mod->where($today_wh)->field('id,pic_url,title,coupon_price,price,quan,shop_type,volume,add_time,isq')->order('is_commend desc,id desc,volume desc')->limit(5)->select();
        $this->assign('today_list', $today_list);
        $where = array(
            'pass' => 1,
            'isshow' => 1,
            'status' => 'underway'
        );
        $count = $this->_mod->where($where)->count();
        $this->assign('total_item', $count);
        $order = '(quan/price)*100 DESC,ordid asc';
        $jingxuan = $this->_mod->where($where)->field('id,pic_url,title,coupon_price,price,quan,shop_type,volume,add_time,isq')->order($order)->limit(5)->select();
        $this->assign('jingxuan', $jingxuan);
        $top = $this->_mod->where($where)->field('id,pic_url,title,coupon_price,price,quan,shop_type,volume,add_time,isq')->order('volume desc')->limit(6)->select();
        $this->assign('top', $top);


        if (C('yh_index_cids')) {
            $cate_show = C('yh_index_cids');
            $product = array();
            $k = 0;

            $cateinfo = $this->_cate_mod->field('name,remark,id')->select();
            foreach ($cateinfo as $c) {
                $cate[$c['id']]['id'] = $c['id'];
                $cate[$c['id']]['name'] = $c['name'];
                $cate[$c['id']]['remark'] = $c['remark'];
            }
            $where = array(
                'pass' => 1,
                'isshow' => 1,
                'status' => 'underway'
            );

            $field = 'id,cate_id,title,pic_url,price,quan,coupon_price,shop_type,isq';
            $sql = '';
            $si = 0;
            foreach ($cate_show as $s) {
                if ($si == 0) {
                    $sql = '(SELECT ' . $field . ' from ' . C("DB_PREFIX") . 'items where cate_id=' . $s . ' and pass=1 and isshow=1 and status="underway" limit 7)';
                } else {
                    $sql = $sql . ' union all (SELECT ' . $field . ' from ' . C("DB_PREFIX") . 'items where cate_id=' . $s . ' and pass=1 and isshow=1 and status="underway" limit 7)';
                }
                $si++;
            }
            $Model = M();
            $list = $Model->cache(true, 5 * 60)->query($sql);
            $cateid = 0;
            foreach ($list as $h) {
                if ($cateid != $h['cate_id']) {
                    $hotsale[$h['cate_id']]['id'] = $h['id'];
                    $hotsale[$h['cate_id']]['title'] = $h['title'];
                    $hotsale    [$h['cate_id']]['pic_url'] = $h['pic_url'];
                }
                $cateid = $h['cate_id'];
            }
            $pi = 0;
            foreach ($list as $p) {
                $newsale[$p['cate_id']][$pi]['price'] = $p['price'];
                $newsale[$p['cate_id']][$pi]['quan'] = $p['quan'];
                $newsale    [$p['cate_id']][$pi]['coupon_price'] = $p['coupon_price'];
                $newsale[$p['cate_id']][$pi]['shop_type'] = $p['shop_type'];
                $newsale    [$p['cate_id']][$pi]['id'] = $p['id'];
                $newsale    [$p['cate_id']][$pi]['title'] = $p['title'];
                $newsale[$p['cate_id']][$pi]['pic_url'] = $p['pic_url'];
                $pi++;
            }
            foreach ($cate_show as $v) {
                $product[$k]['hotsale'] = $hotsale[$v];
                $product[$k]['newsale'] = $newsale[$v];
                $product[$k]['name'] = $cate[$v]['name'];
                $product[$k]['cid'] = $cate[$v]['id'];
                $product[$k]['remark'] = $cate[$v]['remark'];
                $k++;
            }

            $this->assign('hotsales', $product);

        }
        $article_list = M('article')->where('status=1')
            ->order('ordid asc,id desc')
            ->field('title,cate_id,add_time,id,pic,info')
            ->limit(4)
            ->select();
        if ($article_list) {
            $goodslist = array();
            foreach ($article_list as $k => $v) {
                $goodslist[$k]['id'] = $v['id'];
                $goodslist[$k]['pic'] = $v['pic'];
                $goodslist[$k]['cateid'] = $v['cate_id'];
                $goodslist[$k]['title'] = $v['title'];
                $goodslist[$k]['add_time'] = date('Y-m-d', $v['add_time']);
                $goodslist[$k]['infocontent'] = cut_html_str($v['info'], 80);
                if (C('APP_SUB_DOMAIN_DEPLOY')) {
                    $goodslist[$k]['linkurl'] = '/article/view_' . $v['id'];
                } else {
                    $goodslist[$k]['linkurl'] = U('/article/read', array('id' => $v['id']));
                }

            }
            $this->assign('list', $goodslist);
        }

        $this->_config_seo(C('yh_seo_config.index'));
        $this->display();
    }


}